package br.com.revenda.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import br.com.revenda.conexao.conectionFactory;
import br.com.revenda.modelo.Fornecedor;

public class FornecedorDAO {
	
	Connection con;
	
	public FornecedorDAO(){
		con = new conectionFactory().getConnection();
	}
	
	
	
	public void salvar(Fornecedor f){
		String sql = "insert into fornecedor (nome ,cnpj , ie , fone , celular , email , site , cep , endereco , cidade , estado , contato)values(?,?,?,?,?,?,?,?,?,?,?,?)";
		try {
			PreparedStatement pstm = con.prepareStatement(sql);
			pstm.setString(1, f.getNome());
			pstm.setString(2, f.getCnpj());
			pstm.setString(3, f.getIe() );
			pstm.setString(4, f.getFone());
			pstm.setString(5, f.getCelular());
			pstm.setString(6, f.getEmail());
			pstm.setString(7, f.getSite());
			pstm.setString(8, f.getCep());
			pstm.setString(9, f.getEndereco());
			pstm.setString(10, f.getCidade());
			pstm.setString(11, f.getEstado());
			pstm.setString(12, f.getContato());
			pstm.execute();
			pstm.close();
			con.close();			
		} catch (SQLException e) {
			e.printStackTrace();
		} 
	}
	
	
	public void alterar(Fornecedor f){
		String sql = "update fornecedor set nome=? ,cnpj=? , ie=? , fone=? , celular=? , email=? , site=? , cep=? , endereco=? , cidade=? , estado=? , contato=? where idFornecedor=?";
		try {
			System.out.println(f.getIdFornecedor());
			PreparedStatement pstm = con.prepareStatement(sql);
			pstm.setString(1, f.getNome());
			pstm.setString(2, f.getCnpj());
			pstm.setString(3, f.getIe() );
			pstm.setString(4, f.getFone());
			pstm.setString(5, f.getCelular());
			pstm.setString(6, f.getEmail());
			pstm.setString(7, f.getSite());
			pstm.setString(8, f.getCep());
			pstm.setString(9, f.getEndereco());
			pstm.setString(10, f.getCidade());
			pstm.setString(11, f.getEstado());
			pstm.setString(12, f.getContato());
			pstm.setInt(13, f.getIdFornecedor());
			pstm.execute();
			pstm.close();
			con.close();			
		} catch (SQLException e) {
			e.printStackTrace();
		} 
	}
	
	
	public void deletar(Fornecedor f){
		String sql = "delete from fornecedor where idFornecedor=?";
		try {
			System.out.println(f.getIdFornecedor());
			PreparedStatement pstm = con.prepareStatement(sql);
			pstm.setInt(1, f.getIdFornecedor());
			pstm.execute();
			pstm.close();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public List<Fornecedor> listaTodos(){
		List<Fornecedor> lista = new ArrayList<Fornecedor>();
		String sql = "select * from fornecedor";
		try {
			PreparedStatement pstm = con.prepareStatement(sql);
			ResultSet rs = pstm.executeQuery();
			
			while(rs.next()){
				Fornecedor f = new Fornecedor();
				f.setNome(rs.getString("nome"));
				f.setCnpj(rs.getString("cnpj"));
				f.setIe(rs.getString("ie"));
				f.setFone(rs.getString("fone"));
				f.setCelular(rs.getString("celular"));
				f.setEmail(rs.getString("email"));
				f.setSite(rs.getString("site"));
				f.setCep(rs.getString("cep"));
				f.setEndereco(rs.getString("endereco"));
				f.setCidade(rs.getString("cidade"));
				f.setEstado(rs.getString("estado"));
				f.setContato(rs.getString("contato"));
				f.setIdFornecedor(rs.getInt("idFornecedor"));
				lista.add(f);
			}
			rs.close();
			pstm.close();
			con.close();			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return lista;
	}
	
	

}
